package ja12resttemplate;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class RestTemplateUtil {

    public static String restCommon(String url , Map<String,Object> params){
        params = new HashMap<>(2);
        url = "http://localhost:8080/imcpsei-infser/infserapi005/api/01?apikey=rY6OHHfqOqArk%2BqUeUn%2B4tJhNu82%2FYokDvmWol%2Bg1MzEHc9lfFLzYfsBtpKFXVcYDgu0yp3LkcNpquj%2B8z4Iyc5kJ9%2B9hXUXBoFvKX4fG%2FPRqRPAHnZ2mvRjbAdeKD%2B3nfJ7Ota6ILZspH%2Ff8mLtYe6qHwXKA9Fz%2FPWeW%2BQPvBA%3D";
        params.put("orgNo","91210122MA0QFQAK05_SYHC");

        String rootUrl = "http://localhost:8080/test/";
        rootUrl = url;
        RestTemplate restTemplate = new RestTemplate();
//        RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
//        RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());

        //1. 简单Get请求
        String result = restTemplate.getForObject(rootUrl, String.class);
        System.out.println("简单Get请求:" + result);

        //2. 简单带路径变量参数Get请求
        result = restTemplate.getForObject(rootUrl, String.class, 239);
        System.out.println("简单带路径变量参数Get请求:" + result);

        //3. 返回对象Get请求（注意需包含compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'）
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(rootUrl, String.class);
        System.out.println("返回:" + responseEntity);
        System.out.println("返回对象Get请求:" + responseEntity.getBody());

        //4. 设置header的Get请求
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", "123");
        ResponseEntity<String> response = restTemplate.exchange(rootUrl, HttpMethod.GET, new HttpEntity<String>(headers), String.class);
        System.out.println("设置header的Get请求:" + response.getBody());

        //5. Post对象
        result = restTemplate.postForObject(rootUrl, params, String.class);
        System.out.println("Post对象:" + result);

        //6. 带header的Post数据请求
        response = restTemplate.postForEntity(rootUrl , new HttpEntity<Map<String, Object>>(params, headers), String.class);
        System.out.println("带header的Post数据请求:" + response.getBody());

        //7. 带header的Put数据请求
        //无返回值
        restTemplate.put(rootUrl , new HttpEntity<Map<String, Object>>(params, headers));
        //带返回值
        response = restTemplate.exchange(rootUrl , HttpMethod.PUT, new HttpEntity<Map<String, Object>>(params, headers), String.class);
        System.out.println("带header的Put数据请求:" + response.getBody());

        //8. del请求
        //无返回值
        restTemplate.delete(rootUrl , 332);
        //带返回值
        response = restTemplate.exchange(rootUrl, HttpMethod.DELETE, null, String.class);
        System.out.println("del数据请求:" + response.getBody());

        return null;
    }

}
